package top.llove.utils.time;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {
    private static final Calendar CALENDAR = Calendar.getInstance();
    private static final SimpleDateFormat SDF = new SimpleDateFormat();
    private static final Date DATE = new Date();


    /**
     * 获取指定日期的时间，
     * @param day 0为当前时间,1为明日时间
     * @param format 日期格式
     * @return
     */
    public static String getTime(int day, String format) {
        CALENDAR.setTime(new Date());
        int i = CALENDAR.get(Calendar.DATE);

        CALENDAR.set(Calendar.DATE, i + day);
        SDF.applyPattern(format);

        return SDF.format(CALENDAR.getTime());
    }

    /**
     * 获取指定日期的时间, 默认格式为：yyyy-MM-dd
     * @param day 0为当前时间,1为明日时间
     * @return
     */
    public static String getTime(int day) {
        CALENDAR.setTime(new Date());
        int i = CALENDAR.get(Calendar.DATE);

        CALENDAR.set(Calendar.DATE, i + day);
        SDF.applyPattern("yyyy-MM-dd");
        return SDF.format(CALENDAR.getTime());
    }

    /**
     * 根据指定格式获取当前时间
     * @param format
     * @return
     */
    public static String getTime(String format) {

        CALENDAR.setTime(new Date());
        int i = CALENDAR.get(Calendar.DATE);

        CALENDAR.set(Calendar.DATE, i);
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(CALENDAR.getTime());
    }

    /**
     * 获取当前时间，默认格式为：yyyy-MM-dd
     * @return
     */
    public static String getTime() {
        CALENDAR.setTime(new Date());
        int i = CALENDAR.get(Calendar.DATE);

        CALENDAR.set(Calendar.DATE, i);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(CALENDAR.getTime());
    }

    /**
     * 格式化时间
     * @param date 要格式化的时间对象
     * @param format 格式化字符串
     * @return
     */
    public static String getTime(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 将时间毫秒值转换为指定格式的时间
     * @param time 时间毫秒值
     * @param format 格式化字符串
     * @return
     */
    public static String getTime(Long time, String format) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

}
